import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {ActivatedRoute, NavigationEnd, NavigationStart, Router} from '@angular/router';
import {UtilityService} from '../../../../service/utils.service';
import {appConfig} from '../../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {FuncModule} from "../../../../service/function/func.model";
import {FuncattrModule} from "../../../../service/common.module";
import {CommondictServe} from '../../../../service/commonDict';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';
import {PlatformLocation} from '@angular/common'
import {ReuseTabService} from '@delon/abc';

@Component({
  selector: 'app-func-active',
  templateUrl: './func-active.component.html',
})
export class FuncActiveComponent implements OnInit {

    constructor(
        private http: _HttpClient,
        private router: Router,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        private modal: NzModalService,
        private nznot: NzNotificationService,
        private utilityService: UtilityService,
        private commondict: CommondictServe,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
        location: PlatformLocation,
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) {
        // 浏览器点击后退事件
        location.onPopState(() => {
                this.router.navigateByUrl('/APPlication');
        });
    }
    funGuid: string; // 功能的id
    appGuid: string; // 应用guid
    modalVisible  = false; // 弹出框默认不打开
    activeModal = false; // 默认列表弹出框不打开
    activeAddModal = false; // 默认列表弹出框不打开
    loading = false;
    expandForm = false;
    isEdit = false; // 是否是修改，默认不是
    isSlash  = false;
    funTitle: string; // 功能弹窗标题
    pageIndex = 1; // 当前页数
    configTitle: any;
    activelistTitle: string; // 行为列表弹框标题
    attrlistTitle: string; // 属性列表弹框标题
    activeModelOpen: boolean;
    selectedRows: any; // 总个数
    isopen:boolean;
    ischeck:boolean;
    queryGuid:any;
    queryAtrGuid : any;
    funcName:string;
    attrKey:string;
    // 列表数据
    data: any[] = []; // 表格数据
    headerData = [  // 配置表头内容
        { value: '功能代码', key: 'funcCode', isclick: false },
        // { value: '功能名称', key: 'funcName', isclick: false },
        { value: '功能名称', key: 'funcNames', isclick: false },
        // { value: '功能类型', key: 'funcType', isclick: false },
        { value: '是否启用', key: 'isopen', isclick: false },
        { value: '是否验证权限', key: 'ischeck', isclick: false },
        { value: '显示顺序', key: 'displayOrder', isclick: false },
        // { value: '功能描述', key: 'funcDesc', isclick: false },
        { value: '功能描述', key: 'funcDescs', isclick: false },
    ];
    // 传入按钮层
    moreData = {
        morebutton: true,
        buttons: [
            { key: 'Overview', value: '查看概况' }
        ]
    }
    test: string;
    page: any;
    funcLs: string;
    total: number;
    buttons = [
        {key: 'add', value: '新增功能', buttonCode: appConfig.funcObject.app.appFunc}
    ]
    // 基础数据 后期从后台获取
    funcTypes: any;
    // 是否启用
    isOpen: any;
    ifopen = false; // 默认是不禁止选择的

    isCheck: any;

    // 行为的数据类型
    activeItem: FuncattrModule = new FuncattrModule();
    // 功能的数据值
    funcItem: FuncModule = new FuncModule();
    funcAdd: FuncModule = new FuncModule();
    funcActiveItem: FuncModule = new FuncModule();
    codes: any;
    appName: string;
    ngOnInit() {
        this.appGuid = this.activatedRoute.snapshot.params.id; // 拿到父组件传过来的组织机构的guid来进行操作
        this.codes = JSON.stringify(this.tokenService.get().codeList);
        this.queryAppId();
        this.configTitle = {value: '修改', buttonCode: appConfig.funcObject.app.editFunc};
        this.getData();
        this.dictQueryKey();
    }


    // id 查询应用信息
    queryAppId() {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.acappAdd + '/' + this.appGuid)
            .subscribe(
                (val) => {
                    console.log(val.result);
                    this.appName = val.result.appName;
                    this.reuseTabService.title = this.appName + '的360'
                }
            );
    }


    // 根据key查询业务字典
    dictQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.funcType)
            .subscribe((val) => {
                this.funcTypes = val.result;
                console.log(this.funcTypes)
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.dictYon)
            .subscribe((val) => {
                this.isOpen = val.result;
                this.isCheck = val.result;
            })
    }


    // 功能新增方法
    // 列表的方法
    addHandler(event) {
        this.isAdd = true;
        if (event === 'add') {
            this.funTitle = '新增功能';
            this.funcAdd = new FuncModule();
            this.funcAdd.isopen = false;
            this.funcAdd.ischeck = false
            this.isopen = false;
            this.ischeck = false;
            this.ifopen = true
            this.isEdit = false;
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            // 枚举值转换方法
            event.ischeck = appConfig.comFunc.isYf(event.ischeck)
            event.isopen = appConfig.comFunc.isYf(event.isopen)
            this.funTitle = '修改功能';
            this.isEdit = true;
            this.ifopen = false; // 不禁选
            this.funcAdd = event;
        }
        this.funcAdd.funcType = 'F';
        this.isSlash = false; // 默认不出现
        this.modalVisible = true;  // 此时点击了列表组件的新增，打开模态框
    }


    funcCodeexit(parameter) {
        let objJson = {
            serviceEntry: parameter,
            guid: this.funcAdd.guid,
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.existPathParameter, objJson)
            .subscribe(
                (sus) => {
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

    // 列表传入的翻页数据
    monitorHandler(event) {
        this.pageIndex = event;
        this.getData();
    }

    // 接受子组件删除的数据 单条还是多条
    deleatData(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除功能吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                // 模拟接口
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.funcDel + '/' + event)
                    .subscribe(
                        (val) => {
                            // 修改成功只和的处理逻辑
                            this.nznot.create('success', val.msg , val.msg);
                            if ( !(( this.total - 1) % 10)) {
                                // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                this.funcItem.pi -- ;
                                this.attrDate();
                            }

                            this.getData();
                        });
            },
            onCancel: () => {

            }
        });

    }

    // 功能批量删除方法
    funcbatchDel(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条功能吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchDeleteFunc, deletaObj )
                    .subscribe(
                        (val) => {
                            if ( !(( this.total - 1) % 10)) {
                                 if ( !(( this.total - this.acfundata.length) % 10)) {
                                     this.funcItem.pi -- ;
                                     this.getData()
                                 }// 支持批量删除的方法
                                this.getData();
                            }

                            this.getData();
                            this.selectedRows = [];
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.selectedRows = [];
                this.getData()
            }
        });
    }

    // 列表按钮方法
    buttonDataHandler(event) {
        console.log(event); // 根据event.value来判断不同的请求，来获取结果和方法或者进行路由的跳转
    }



    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {

    }


    selectedRow(event) { // 选中方法，折叠层按钮显示方法

    }


    objJson = {};
    // 搜索框
    search() {
        this.objJson = this.funcItem;
        this.getData(this.funcItem);
    }

    reset() {
        this.funcItem = new FuncModule();
        this.objJson = {};
        this.getData();
    }
    // 根据id查询内容
    getData(options?) {
        if (options) {
            this.pageIndex = 1;
        }

        // 查询功能列表信息
        this.page = {
            condition: this.objJson, // 搜索内容
            page: {
                current: this.pageIndex,
                size: this.funcItem.size,
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.pageQueryFuncByAppId + '/' + this.appGuid, this.page)
            .subscribe(
                (val) => {
                    for (let i = 0; i < val.result.records.length; i++ ) {
                        this.funcLs = val.result.records[i].funcDesc;
                        if(this.funcLs.length> 20){
                            val.result.records[i].funcDescs = (this.funcLs.substr(0,10)+"..."+this.funcLs.substr((this.funcLs.length-10),this.funcLs.length));
                        }else{
                            val.result.records[i].funcDescs = this.funcLs;
                        }
                        this.funcLs = val.result.records[i].funcName;
                        if(this.funcLs.length>20){
                            val.result.records[i].funcNames = (this.funcLs.substr(0,10)+"..."+this.funcLs.substr((this.funcLs.length-10),this.funcLs.length));
                        }else{
                            val.result.records[i].funcNames = this.funcLs;
                        }
                        val.result.records[i].buttonData = [{key: 'dels', value: '删除', if: false,buttonCode:appConfig.funcObject.app.delFuncAttr},{key: 'activeLook', value: '行为', if: false,buttonCode:appConfig.funcObject.app.queryActive}, {key: 'attrLook', value: '属性', if: false,buttonCode:appConfig.funcObject.app.queryAttr}];
                    }
                    this.data = val.result.records;
                    this.total = val.result.total;
                }
            );
    }

    // 按钮点击事件
    buttonEvent(event) {
        this.funGuid = event.guid;
        this.queryAtrGuid = event.guid;
        if(event.names.key === 'attrLook') {
            // console.log("功能下的"+this.funGuid)
            this.queryGuid = this.funGuid;
            this.activeModelOpen = false; // 功能属性关闭方法
            this.attrlistTitle = '功能 (' + event.funcName + ')下的属性列表'
            this.configTitleAttr = {value: '修改', buttonCode: appConfig.funcObject.app.editFuncAttr}
            this.attrDate(); // 查询属性列表
            this.attrindex = 1; // 重置为1
            this.activeModal = true;
        } else if( event.names.key === 'activeLook') {
            // console.log("行为列表"+this.funGuid)
            this.activelistTitle =  '功能 (' + event.funcName + ')下的行为列表'
            this.queryactiveModal = true;
            this.bahIndex = 1; // 重置为1
            this.queryAtrGuid = event.guid;
            this.queryActive(event.guid); // 查询行为
        } else if(event.names.key === 'dels') {
            this.deleatData(event.guid)
        }

    }


    functcancel() {
        this.isAdd = false; // 默认是false
        this.modalVisible = false;
        for(let i = 0; i < this.data.length; i ++ ) {
            if(this.data[i].ischeck === 'Y') {
                this.data[i].ischeck = '是'
            } else {
                this.data[i].ischeck = '否'
            }
            if(this.data[i].isopen === 'Y') {
                this.data[i].isopen = '是'
            } else {
                this.data[i].isopen = '否'
            }
        }
    }


    // 保存方法
    save(isadd) {
        // console.log("-----------------------------------------------------------------")
        // console.log(this.funcAdd)
        if(this.isAdd) {
            const jsonObj = this.funcAdd;
            jsonObj.guidApp = this.appGuid;
            // 新增功能、修改功能逻辑
            if (!this.isEdit) {  // 新增的业务逻辑
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.funcAdd, jsonObj)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getData();
                            this.modalVisible = false;
                        },
                        (error) => {
                            this.nznot.create('success', error.msg , error.msg);
                            this.getData();
                        }
                    );

            } else if(this.isEdit) {
                // 修改的保存逻辑
                this.utilityService.putData(appConfig.serverUrl + appConfig.API.funcDel, jsonObj)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getData();
                            this.modalVisible = false;
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                            this.getData();
                        });
            }
        }

    }




    // 行为属性方法内容
    acfundata: any[] = []; // 表格数据
    // 弹窗功能行为
    activeData = [  // 配置表头内容
        { value: '属性类型', key: 'attrType', isclick: false },
        { value: '属性名', key: 'attrKey', isclick: false },
        { value: '属性值', key: 'attrValue', isclick: false },
        { value: '备注', key: 'memo', isclick: false }
    ];
    activeTitle: string; // 列表标题
    configTitleAttr: any;
    ActriceData = {
        morebutton: true,
        buttons: [
            { key: 'Overview', value: '行为概况' }
        ]
    };
    activebuttons = [
        {key: 'add', value: '新增属性', buttonCode: appConfig.funcObject.app.addActiveAttr}
    ]
    attrindex = 1; // 属性翻页默认变量
    // 列表翻页方法
    activeHandler(event) {
        this.attrindex = event;
        this.actarrtIndex = event;
        if(!this.activeModelOpen) {
            this.attrDate();
        } else {
            this.ActiveattrDate(); // 查询行为下属性列表
        }


    }
    isAdd: boolean; // 行为新增、修改控制
    // 行为列表方法
    attrDate = function () {
        // 查询功能列表信息
        this.page = {
            page: {
                current: this.attrindex,
                size: this.activeItem.size, // 每页个数
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.acFuncList + '/' + this.funGuid, this.page)
            .subscribe(
                (val) => {
                    this.acfundata = val.result.records;
                    // 后台没有翻译， 应该翻译好的
                    for (let i = 0; i <  this.acfundata.length; i ++ ) {
                        if (this.acfundata[i].attrType === 'B') {
                            this.acfundata[i].attrType = '行为';
                        } else {
                            this.acfundata[i].attrType = '功能';
                        }
                        this.acfundata[i].buttonData = [ {key: 'dels', value: '删除属性', if: false,buttonCode :appConfig.funcObject.app.delActiveAttr}];
                    }
                    this.total = val.result.total;
                }
            );
    }


    // 行为弹框新增方法
    addActives(event) {
        this.isAdd = true;
        // 打开弹窗就查询
        if (event === 'add') {
            this.activeTitle = '新增属性';
            this.activeItem = new FuncattrModule();
            this.isEdit = false;
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            this.activeTitle = '修改属性';
            this.isEdit = true;
            this.activeItem = event;
        }
        this.activeItem.attrType = 'B';
        this.activeModal = false;
        this.activeAddModal = true;  // 此时点击了列表组件的新增，打开模态框
    }



    // 行为属性保存方法
    activeSave() {
        if(this.isAdd) { // 是功能的新增
            if (!this.activeModelOpen) {
                const jsonObj = this.activeItem;
                jsonObj.guidFunc = this.funGuid;
                if (!this.isEdit) {  // 新增的业务逻辑
                    this.funcItem.guidApp = this.appGuid;
                    this.utilityService.postData(appConfig.serverUrl + appConfig.API.acFuncAttr, jsonObj)
                        .subscribe(
                            (val) => {
                                this.nznot.create('success', val.msg , val.msg);
                                this.attrDate(); // 新增总是回到第一页，跟数据有关，可以参考删除的写法
                                this.activeAddModal = false;
                                this.activeModal = true;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                                this.attrDate(); // 查询新增行为下属性列表的方法
                            }
                        );
                } else {
                    // 修改的保存逻辑
                    this.utilityService.putData(appConfig.serverUrl + appConfig.API.acFuncPut, jsonObj)
                        .subscribe(
                            (val) => {
                                this.nznot.create('success', val.msg , val.msg);
                                this.attrDate(); // 查询新增行为下属性列表的方法
                                this.activeAddModal = false;
                                this.activeModal = true;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                                this.attrDate(); // 查询新增行为下属性列表的方法
                            });
                }
            } else { // 是行为的属性新增
                const jsonObj = this.activeItem;
                jsonObj.guidFunc = this.activeGuid;
                if (!this.isEdit) {  // 新增的业务逻辑
                    this.funcItem.guidApp = this.appGuid;
                    this.utilityService.postData(appConfig.serverUrl + appConfig.API.addBehave, jsonObj)
                        .subscribe(
                            (val) => {
                                this.nznot.create('success', val.msg , val.msg);
                                this.ActiveattrDate(); // 查询新增行为下属性列表的方法
                                this.activeAddModal = false;
                                this.activeModal = true;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                                this.ActiveattrDate(); // 查询新增行为下属性列表的方法
                            }
                        );
                } else {
                    // 修改的保存逻辑
                    this.utilityService.putData(appConfig.serverUrl + appConfig.API.updateAttrBehave, jsonObj)
                        .subscribe(
                            (val) => {
                                this.nznot.create('success', val.msg , val.msg);
                                this.ActiveattrDate(); // 新增总是回到第一页，跟数据有关，可以参考删除的写法
                                this.activeAddModal = false;
                                this.activeModal = true;
                            },
                            (error) => {
                                this.nznot.create('error', error.msg , error.msg);
                                this.ActiveattrDate(); // 新增总是回到第一页，跟数据有关，可以参考删除的写法
                            });
                }

            }

        }
    }

    // 行为属性取消方法
    acTiveCancel() {
        this.activeAddModal = false;  // 关闭行为新增弹窗
        this.activeModal = true;    // 打开行为列表弹窗
        this.isAdd = false; // 不调用新增修改方法
    }

    // 属性弹窗关闭方法
    activeModel() {
        if(this.activeModelOpen) { // 行为属性，关闭弹窗，打开行为弹窗
            this.activeModal = false;
            this.queryactiveModal = true;
        } else {
            this.activeModal = false;
        }
    }

    // 列表弹窗删除属性方法
    deleatActiveData(event) {
        this.activeModal = false;
        if(!this.activeModelOpen) { // 是功能的删除
            this.modal.open({
                title: '是否删除',
                content: '您是否确认删除该属性吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    // 模拟接口
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.acFuncDel + '/' + event.guid)
                        .subscribe(
                            (val) => {
                                // 修改成功只和的处理逻辑
                                this.nznot.create('success', val.msg , val.msg);
                                if ( !(( this.total - 1) % 10)) {
                                    // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                    this.activeItem.pi -- ;
                                }
                                this.attrDate();
                                this.activeModal = true;
                            });
                },
                onCancel: () => {
                    this.activeModal = true;
                    this.attrDate();
                }
            });
        } else {
            this.modal.open({
                title: '是否删除',
                content: '您是否确认删除该属性吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    // 模拟接口
                    this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.acFuncDel + '/' + event.guid)
                        .subscribe(
                            (val) => {
                                // 修改成功只和的处理逻辑
                                this.nznot.create('success', val.msg , val.msg);
                                if ( !(( this.total - 1) % 10)) {
                                    // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                    this.activeItem.pi -- ;
                                }
                                this.ActiveattrDate();
                                this.activeModal = true;
                            });
                },
                onCancel: () => {
                    this.activeModal = true;
                    this.ActiveattrDate();
                }
            });
        }
    }

    // 批量删除属性方法
    attrbatchDel(event) {
        this.activeModal = false;
        if(!this.activeModelOpen) { // 是功能的删除
            this.modal.open({
                title: '是否删除',
                content: '您是否确认删除这' + event.length + '条属性吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    let delarray = [];
                    for( let i = 0; i < event.length; i++) {
                        delarray.push(event[i].guid);
                    }
                    let deletaObj = {
                        deleteList: delarray
                    }
                    this.utilityService.postData(appConfig.serverUrl + appConfig.API.attrbatchDelete, deletaObj )
                        .subscribe(
                            (val) => {
                                // 修改成功只和的处理逻辑
                                this.nznot.create('success', val.msg , val.msg);
                                if ( !(( this.total - 1) % 10)) {
                                    // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                    this.activeItem.pi -- ;
                                }
                                this.attrDate();
                                this.selectedRows = [];
                                this.activeModal = true;
                            });
                },
                onCancel: () => {
                    this.activeModal = true;
                    this.selectedRows = [];
                    this.attrDate();
                }
            })
        } else {
            this.modal.open({
                title: '是否删除',
                content: '您是否确认删除这' + event.length + '条数据吗?',
                okText: '确定',
                cancelText: '取消',
                onOk: () => {
                    let delarray = [];
                    for( let i = 0; i < event.length; i++) {
                        delarray.push(event[i].guid);
                    }
                    let deletaObj = {
                        deleteList: delarray
                    }
                    this.utilityService.postData(appConfig.serverUrl + appConfig.API.attrbatchDelete, deletaObj )
                        .subscribe(
                            (val) => {
                                // 修改成功只和的处理逻辑
                                this.nznot.create('success', val.msg , val.msg);
                                if ( !(( this.total - 1) % 10)) {
                                    // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                    this.activeItem.pi -- ;
                                }
                                this.ActiveattrDate();
                                this.activeModal = true;
                            });
                },
                onCancel: () => {
                    this.activeModal = true;
                    this.ActiveattrDate();
                }
            })
        }


    }

    // 列表页右侧方法
    activeButton(event) {
        if(event.names.key === 'dels') {
            this.deleatActiveData(event);
        }
    }



    // 行为方法
    acfunActivedata = []; // 行为数据
    activeText: string; // 行为弹框名称
    queryactiveModal = false; // 行为列表弹框
    modalActiveVisible = false; // 行为新增弹框
    addActiveButtons = [
        {key: 'add', value: '新增行为', buttonCode: appConfig.funcObject.app.addACtive}
    ]
    activesTitle =  {value: '修改', buttonCode: appConfig.funcObject.app.editACtive}
    isActiveOpen = false; // 是否禁选
    isactiveEdit: boolean; // 是新增行为还是修改行为判断变量
    ActiveheaderData = [  // 配置表头内容
        { value: '行为代码', key: 'funcCode', isclick: false },
        { value: '行为名称', key: 'funcName', isclick: false },
        { value: '是否启用', key: 'isopen', isclick: false },
        { value: '是否验证权限', key: 'ischeck', isclick: false },
        { value: '显示顺序', key: 'displayOrder', isclick: false },
        { value: '功能描述', key: 'funcDesc', isclick: false },
    ];
    activeGuid: string; // 行为的guid
    bahIndex = 1;
    // 查询行为接口
    queryActive(item) {
        this.queryGuid = item;
        this.page = {
            page: {
                current: this.bahIndex,
                size: this.activeItem.size, // 每页个数
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.pageQueryBehaveByFuncId + '/' + item, this.page)
            .subscribe(
                (val) => {
                    // 后台没有翻译， 应该翻译好的
                    for (let i = 0; i < val.result.records.length; i++ ) {
                        val.result.records[i].buttonData = [{key: 'dels', value: '删除', if: false,buttonCode : appConfig.funcObject.app.delActiveAttr}, {key: 'attr', value: '属性', if: false, buttonCode: appConfig.funcObject.app.queryActiveAttr}];
                    }
                    this.acfunActivedata = val.result.records;
                    console.log(this.acfunActivedata);
                    this.total = val.result.total;
                }
            );
    };

    // 新增行为接口
    addActivesHandler(event) {
        this.isAdd = true;
        if (event === 'add') {
            this.funcActiveItem = new FuncModule();
            this.isActiveOpen = true;
            this.isactiveEdit = false; // 是新增,不是修改
            this.funTitle = '新增行为';
            this.funcActiveItem.isopen = 'Y'
            this.funcActiveItem.ischeck = 'Y'
            this.queryactiveModal = false; // 关闭查询列表
            this.modalActiveVisible = true;  // 打开新增列表
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            this.isactiveEdit = true; // 是修改,不是新增
            this.funTitle = '修改行为';
            event.ischeck = appConfig.comFunc.isYf(event.ischeck)
            event.isopen = appConfig.comFunc.isYf(event.isopen)
            this.isActiveOpen = false; // 不禁用
            this.queryactiveModal = false; // 关闭查询列表
            this.modalActiveVisible = true; // 打开新增行为列表
            this.funcActiveItem = event;
        }
        this.funcActiveItem.funcType = 'B'; // 默认是行为
    }

    // 行为翻页
    addActiveHandler(event) {
        this.bahIndex = event;
        this.queryActive(this.funGuid);
    }


    activeButonEvent(event) {
        if(event.names.key === 'dels') {
            this.DelActive(event.guid)
        } else {
            this.queryAtrGuid = event.guid;
            this.activeModal = true; // 属性打开
            this.queryactiveModal = false; // 行为列表弹窗关闭
            this.attrlistTitle = '行为 (' + event.funcName + ')下的属性列表'
            this.actarrtIndex = 1; // 重置为1
            this.activeModelOpen = true; // 行为属性关闭方法
            this.ActiveattrDate(); // 查询行为下属性
        }
    }

    // 新增、修改行为方法
    modalActiveVisiblesave() {
        if (this.isAdd) { // 因为返回也会进入方法，用isAdd来控制
            const jsonObj = this.funcActiveItem;
            jsonObj.guidFunc = this.funGuid;
            // 枚举值转换
            // 新增功能、修改功能逻辑
            this.funcActiveItem.guidApp = this.appGuid;
            if (!this.isactiveEdit) {  // 新增的业务逻辑
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.addBehaveFunc, jsonObj)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.queryActive(this.funGuid)
                            this.modalActiveVisible = false;
                            this.queryactiveModal = true;
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                            this.queryActive(this.funGuid)
                        }
                    );
            } else if( this.isactiveEdit) {
                // 修改的保存逻辑
                this.utilityService.putData(appConfig.serverUrl + appConfig.API.updateBehave, jsonObj)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.queryActive(this.funGuid)
                            this.modalActiveVisible = false;
                            this.queryactiveModal = true;
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                            this.queryActive(this.funGuid)
                        });
            }

        }
    }

    // 新增、修改行为取消方法
    ActiveCancel() {
        this.modalActiveVisible = false;
        this.queryactiveModal = true;
        this.isAdd = false;
        for(let i = 0; i < this.acfunActivedata.length; i ++ ) {
            if(this.acfunActivedata[i].ischeck === 'Y') {
                this.acfunActivedata[i].ischeck = '是'
            } else {
                this.acfunActivedata[i].ischeck = '否'
            }
            if(this.acfunActivedata[i].isopen === 'Y') {
                this.acfunActivedata[i].isopen = '是'
            } else {
                this.acfunActivedata[i].isopen = '否'
            }
        }
    }


    // 删除行为方法
    DelActive(event) {
        console.log('刪除行爲還是屬性')
        this.queryactiveModal = false; // 关闭弹窗
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除该行为吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                // 模拟接口
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.funcDel + '/' + event + '/deleteBehave')
                    .subscribe(
                        (val) => {
                            // 修改成功只和的处理逻辑
                            this.nznot.create('success', val.msg , val.msg);
                            if ( !(( this.total - 1) % 10)) {
                                this.funcItem.pi -- ;
                                this.queryActive(this.funGuid)
                            }
                            this.queryactiveModal = true;
                            this.queryActive(this.funGuid)
                        });
            },
            onCancel: () => {
                this.queryactiveModal = true;
                this.queryActive(this.funGuid)
            }

        });
    }

    // 批量删除行为的方法
    activebatchDel(event) {
        this.queryactiveModal = false; // 关闭弹窗
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条行为数据吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray
                }

                this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchDeleteBehave, deletaObj )
                    .subscribe(
                        (val) => {
                            // 修改成功只和的处理逻辑
                            this.nznot.create('success', val.msg , val.msg);
                            if ( !(( this.total - 1) % 10)) {
                                this.funcItem.pi -- ;
                                this.queryActive(this.funGuid)
                            }
                            this.queryactiveModal = true;
                            this.selectedRows = [];
                            this.queryActive(this.funGuid)
                        });
            },
            onCancel: () => {
                this.queryactiveModal = true;
                this.selectedRows = [];
                this.queryActive(this.funGuid)
            }
        });
    }

    switchchange(item, status) {
        if(item === 'isopen') {
            if(status){
                this.funcAdd.isopen = 'Y';
            }else{
                this.funcAdd.isopen = 'N';
            }
        }
        if(item === 'ischeck'){
            if(status) {
                this.funcAdd.ischeck = 'Y';
            }else{
                this.funcAdd.ischeck = 'N';
            }
        }
    }

    // 查询行为下属性
    actarrtIndex = 1; // 默认为第一页
    // 行为列表方法
    ActiveattrDate = function () {
        // 查询功能列表信息
        this.page = {
            condition: this.search,
            page: {
                current: this.actarrtIndex, // 当前页码
                size: this.activeItem.size, // 每页个数
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.acFuncAttr + '/' + this.queryAtrGuid + '/behaveList', this.page)
            .subscribe(
                (val) => {
                    this.acfundata = val.result.records;
                    // 后台没有翻译， 应该翻译好的
                    for (let i = 0; i <  this.acfundata.length; i ++ ) {
                        if (this.acfundata[i].attrType === 'B') {
                            this.acfundata[i].attrType = '行为';
                        } else {
                            this.acfundata[i].attrType = '功能';
                        }
                        this.acfundata[i].buttonData = [ {key: 'dels', value: '删除属性', if: false, buttonCode: appConfig.funcObject.app.delActiveAttr}];
                    }
                    this.total = val.result.total;
                }
            );
    }
    mainSearch(){
        this.activeItem.attrKey = this.attrKey;
        // 查询功能列表信息
            this.page = {
                condition:this.activeItem,
                page: {
                    current: this.attrindex,
                    size: this.activeItem.size, // 每页个数
                }
            };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.acFuncList + '/' + this.queryAtrGuid, this.page)
            .subscribe(
                (val) => {
                    this.acfundata = val.result.records;
                    // 后台没有翻译， 应该翻译好的
                    for (let i = 0; i <  this.acfundata.length; i ++ ) {
                        if (this.acfundata[i].attrType === 'B') {
                            this.acfundata[i].attrType = '行为';
                        } else {
                            this.acfundata[i].attrType = '功能';
                        }
                        this.acfundata[i].buttonData = [ {key: 'dels', value: '删除属性', if: false, buttonCode: appConfig.funcObject.app.delActiveAttr}];
                    }
                    this.total = val.result.total;
                }
            );
    }
    queryMainActive() {
        this.funcItem.funcName = this.funcName;
        this.page = {
            condition:this.funcItem,
            page: {
                current: this.bahIndex,
                size: this.activeItem.size, // 每页个数
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.pageQueryBehaveByFuncId + '/' + this.queryGuid, this.page)
            .subscribe(
                (val) => {
                    // 后台没有翻译， 应该翻译好的
                    for (let i = 0; i < val.result.records.length; i++ ) {
                        val.result.records[i].buttonData = [{key: 'dels', value: '删除', if: false}, {key: 'attr', value: '属性', if: false}];
                    }
                    this.acfunActivedata = val.result.records;
                    this.total = val.result.total;
                }
            );
    };
    resetVal(){
        this.queryAtrGuid = this.queryAtrGuid;
        this.attrKey = '';
        this.mainSearch();
    }
    resetVals(){
        this.funcName = '';
        this.queryMainActive();
    }
}
